package com.malou.client.ventas.modeloprenda;

import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.StoreEvent;
import com.extjs.gxt.ui.client.store.StoreListener;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.NumberFormat;
import com.malou.client.MalouConstants;
import com.malou.client.util.ComboboxFieldBinding;
import com.malou.client.util.Utilities;

public class ModeloPrendaDialog extends Dialog {
	private HiddenField<String> id;
	private TextField<String> nombre;
	private TextField<String> codigo;
	private NumberField precioventa;
	private NumberField precioventaxmayor;
	private ComboBox<BeanModel> tipo;
	private ComboBox<BeanModel> variacion;
	private Button guardar;
	private Button cancelar;
	public Status status;
	private FormPanel panel;

	private FormBinding formBindings;
	private FormButtonBinding formbuttonbinding;

	private MalouConstants constants = GWT.create(MalouConstants.class);

	public ModeloPrendaDialog() {
		super();
		setModal(true);
		setSize(360, 220);
		setResizable(false);
		setButtons("");

		panel = new FormPanel();
		panel.setBorders(false);
		panel.setBodyBorder(false);
		panel.setPadding(5);
		panel.setHeaderVisible(false);

		status = new Status();
		status.setBusy(constants.guardando());
		status.hide();
		status.setAutoWidth(true);

		cancelar = new Button(constants.cancelar());
		cancelar.addSelectionListener(new SelectionListener<ButtonEvent>() {
			public void componentSelected(ButtonEvent ce) {
				ModeloPrendaDialog.this.hide();
			}

		});

		guardar = new Button(constants.guardar());
		guardar.addSelectionListener(new SelectionListener<ButtonEvent>() {
			public void componentSelected(ButtonEvent ce) {
				
				guardar();
				
			}
		});

		formBindings = new FormBinding(panel, false);
		formbuttonbinding = new FormButtonBinding(panel);
		formbuttonbinding.addButton(guardar);

		id = new HiddenField<String>();
		id.setVisible(false);
		panel.add(id);

		ModeloPrendas.getComboTipoPrendas();
		ModeloPrendas.loaderTipoPrenda.load();

		tipo = new ComboBox<BeanModel>();
		tipo.setFieldLabel(constants.modeloPrendaTipo());
		tipo.setDisplayField("nombre");
		tipo.setEmptyText("Seleccione...");
		tipo.setStore(ModeloPrendas.storeTipoPrenda);
		tipo.setTypeAhead(true);
		tipo.setTriggerAction(TriggerAction.ALL);
		tipo.setValueField("nombre");
		tipo.setAllowBlank(false);
		tipo.setUseQueryCache(false);
		tipo.setMinChars(0);
		tipo.addListener(Events.Expand, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				tipo.clearSelections();
			}

		});
		tipo.getStore().addStoreListener(new StoreListener<BeanModel>() {
			public void storeDataChanged(StoreEvent<BeanModel> se) {
				tipo.getStore().filter("nombre", tipo.getRawValue());
			}
		});
		

		ModeloPrendas.getComboVariaciones();
		ModeloPrendas.loaderVariacion.load();

		variacion = new ComboBox<BeanModel>();
		variacion.setFieldLabel(constants.stockModeloPrenda());
		variacion.setDisplayField("descripcion");
		variacion.setEmptyText("Seleccione...");
		variacion.setStore(ModeloPrendas.storeVariacion);
		variacion.setTypeAhead(true);
		variacion.setTriggerAction(TriggerAction.ALL);
		variacion.setValueField("descripcion");
		variacion.setAllowBlank(false);
		variacion.setUseQueryCache(false);
		variacion.setMinChars(0);
		variacion.addListener(Events.Expand, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				variacion.clearSelections();
			}

		});

		variacion.getStore().addStoreListener(new StoreListener<BeanModel>() {
			public void storeDataChanged(StoreEvent<BeanModel> se) {
				variacion.getStore().filter("descripcion", variacion.getRawValue());
			}
		});

		codigo = new TextField<String>();
		codigo.setMinLength(3);
		codigo.setFieldLabel(constants.modeloPrendaCodigo());
		codigo.setReadOnly(true);

		nombre = new TextField<String>();
		nombre.setMinLength(3);
		nombre.setFieldLabel(constants.VariacionNombre());
		nombre.setAllowBlank(false);
		
		precioventa = new NumberField();

		precioventa.setMinLength(2);
		precioventa.setFieldLabel(constants.modeloPrendaPrecioVenta());
		precioventa.setAllowBlank(false);
		precioventa.setMaxValue(100000);
		precioventa.setFormat(NumberFormat.getFormat("0.00"));
		precioventa.setAllowNegative(false);
		

		precioventaxmayor = new NumberField();

		precioventaxmayor.setMinLength(2);
		precioventaxmayor.setFieldLabel(constants.modeloPrendaPrecioVentaxMayor());
		precioventaxmayor.setAllowBlank(false);
		precioventaxmayor.setMaxValue(100000);
		precioventaxmayor.setFormat(NumberFormat.getFormat("0.00"));
		precioventaxmayor.setAllowNegative(false);
		
		
		panel.add(setLayout(tipo, "95%", 90), new ColumnData(1));
		panel.add(setLayout(variacion, "95%", 90), new ColumnData(1));
		panel.add(setLayout(nombre, "95%", 90), new ColumnData(1));
		panel.add(setLayout(precioventa, "50%", 90), new ColumnData(1));
		panel.add(setLayout(precioventaxmayor, "50%", 90), new ColumnData(1));

		add(panel);
		getButtonBar().add(status);
		addButton(guardar);
		addButton(cancelar);
		getButtonBar().add(new FillToolItem());
		setFocusWidget(tipo);

		Component comp[] = { tipo, variacion, nombre, precioventa,precioventaxmayor, guardar };
		Utilities.Enter(comp);
	}

	public void reset() {
		setIconStyle("icon-new");
		setHeading(constants.modeloPrendaNuevo());
		id.clear();
		nombre.clear();
		codigo.clear();
		precioventa.clear();
		precioventaxmayor.clear();
		status.hide();
		
	}

	public void setStore(ListStore<BeanModel> store) {
		formBindings.addFieldBinding(new FieldBinding(id, "id"));
		formBindings.addFieldBinding(new FieldBinding(nombre, "nombre"));
		formBindings.addFieldBinding(new FieldBinding(codigo, "codigo"));
		formBindings.addFieldBinding(new FieldBinding(precioventa,
				"precioventa"));
		formBindings.addFieldBinding(new FieldBinding(precioventaxmayor,
				"precioventaxmayor"));
		formBindings.addFieldBinding(new ComboboxFieldBinding(tipo,
				"tipoprenda"));
		formBindings.addFieldBinding(new ComboboxFieldBinding(variacion,
				"variacion"));

		formBindings.setStore(store);
	}

	public void beforeEdit() {
		setIconStyle("icon-update");
		setHeading(constants.modeloPrendaModificar());
		status.hide();
		
	}

	public void bind(BeanModel selectedModel) {
		formBindings.bind(selectedModel);
	}

	public void unbind() {
		formBindings.unbind();
	}

	private void guardar() {

		if (guardar.isEnabled() == true) {
			status.show();
			
			ModeloPrenda mp = new ModeloPrenda();

			if (id.getValue() != null)
				mp.setId(Integer.valueOf(id.getValue()));

			mp.setNombre(nombre.getValue());
			mp.setCodigo(codigo.getValue());
			if (Utilities.hasValue(precioventa))
				mp.setPrecioventa(Double.valueOf(precioventa.getValue()
						.toString()));
			
			if (Utilities.hasValue(precioventaxmayor))
				mp.setPrecioventaxmayor(Double.valueOf(precioventaxmayor.getValue()
						.toString()));
			
			if (tipo.getValue().get("id") != null)
				mp.setTipoprenda_id(Integer.parseInt(tipo.getValue().get("id")
						.toString()));

			if (variacion.getValue().get("id") != null)
				mp.setVariacion_id(Integer.parseInt(variacion.getValue().get("id")
						.toString()));

			ModeloPrendas.modeloPrendaDialog = ModeloPrendaDialog.this;
			ModeloPrendas.save(mp);
		}

	}

	private LayoutContainer setLayout(Component comp, String porcentaje,
			Integer withlabel) {
		LayoutContainer layout = new LayoutContainer();
		layout.setStyleAttribute("marginTop", "5px");
		FormLayout fl = new FormLayout(LabelAlign.LEFT);
		fl.setLabelWidth(withlabel);
		layout.setLayout(fl);
		layout.add(comp, new FormData(porcentaje));

		return layout;
	}

}
